#include "enbody.h"
#include <enellipseitem.h>
#include <QDebug>

//////////////////////////////
// Static vars and functions
/////////////////////////////

int EnBody::localObjectCounter = 0;

QColor EnBody::colorCache;

void EnBody::parseOscMsg( const char* argument, int newValue, EnBody *body )
{
    if( strcmp( argument, "xCoor" ) == 0 )
    {
        body->getGraphicsItem()->setX( newValue );
    }

    else if( strcmp( argument, "yCoor" ) == 0 )
    {
        body->getGraphicsItem()->setY( newValue );
    }

    else if( strcmp( argument, "red" ) == 0 )
    {
        EnBody::colorCache = body->getBrush().color().toRgb();
        EnBody::colorCache.setRed( newValue );
        body->getGraphicsItem()->brush() = EnBody::colorCache;
    }

    else if( strcmp( argument, "green" ) == 0 )
    {
        EnBody::colorCache = body->getBrush().color().toRgb();
        EnBody::colorCache.setGreen( newValue );
        body->getGraphicsItem()->brush() = EnBody::colorCache;
    }

    else if( strcmp( argument, "blue" ) == 0 )
    {
        EnBody::colorCache = body->getBrush().color().toRgb();
        EnBody::colorCache.setBlue( newValue );
        body->getGraphicsItem()->brush() = EnBody::colorCache;
    }

    else if( strcmp( argument, "alpha" ) == 0 )
    {
        EnBody::colorCache = body->getBrush().color().toRgb();
        EnBody::colorCache.setAlpha( newValue );
        body->getGraphicsItem()->brush() = EnBody::colorCache;
    }

}

////////////////////
// Member functions
///////////////////

EnBody::EnBody( QString key)
{
    mapKey = key;
    int size = ( qrand()%60 ) + 10;
    graphicsItem = new EnEllipseItem( this, 0, 0, size, size );
    graphicsItem->moveBy( qrand()%920, qrand()%700 );
    graphicsItem->setFlag( QGraphicsItem::ItemIsSelectable, true );
    graphicsItem->setFlag( QGraphicsItem::ItemIsMovable, true );

    outlineColor = QColor( 0, 0, 0, 255 );
    pen.setColor( outlineColor );
    graphicsItem->setPen( pen );

    fillColor.setRed( qrand()%50 );
    fillColor.setGreen( qrand()%150 );
    fillColor.setBlue( qrand()%255 );
    fillColor.setAlpha( 100 );
    brush.setColor( fillColor );
    brush.setStyle( Qt::SolidPattern );
    graphicsItem->setBrush( brush );
    graphicsItem->setCacheMode( QGraphicsItem::DeviceCoordinateCache );

}

EnBody::~EnBody()
{
    delete graphicsItem;
}

QBrush EnBody::getBrush()
{
    return brush;
}

QGraphicsEllipseItem* EnBody::getGraphicsItem()
{
    return graphicsItem;
}

void EnBody::updatePos( const QPointF& point )
{
    emit posChanged( mapKey, point );
}

void EnBody::updateColor( const QColor& color )
{
    emit colorChanged( mapKey, color );
}

QString EnBody::getMapKey()
{
    return mapKey;
}

